package com.huifaapp.mall.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Tmall_shopkeeperFragment;
import com.huifaapp.mall.adapter.tmall_teamoneFragmentAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiException;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.TeamBean;
import com.huifaapp.mall.dialogs.LoadingDialog;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by duanyu on 2018/1/5.
 * 团队一级页面
 */

public class tmall_teamoneFragment extends Fragment {

    private View inflate;
    private XRecyclerView tmall_teamoneRecyclerView;
    public  static  String TEAMURL= ApiManager.BASE_NIUPAI_URL+"users/team/list";
    private ArrayList<TeamBean.DataBean.ListBean> teamBeanlist;
    private tmall_teamoneFragmentAdapter tmall_teamoneFragmentAdapter;
    private int start = 0;
    private LinearLayoutManager mLayoutManager;
    private HashMap<String, String> teamMap;
    private EditText tmall_teamone_editText_userName;
    private EditText tmall_teamone_editText_userPhone;
    private EditText tmall_teamone_editText_startTime;
    private EditText tmall_teamone_editText_endTime;
    private Button tmall_teamone_editText_inquire;
    private LinearLayout tmall_shopkeepeper_team_btn;
    LoadingDialog dialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_tmall_teamone, container, false);
        dialog = new LoadingDialog(getActivity());
        dialog.setContent("正在加载数据");
        dialog.show();
        initView();
        initTeamHttp();
        if (tmall_teamone_editText_userPhone.getText().toString()==null){
            initTeamHttp();
//            tmall_teamoneFragmentAdapter.notifyDataSetChanged();
        }

        return inflate;
    }

    private void initTeamInquireHttp() {
        Log.e("我的团队-一级好友sear",TEAMURL+"?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)
                +"&appId="+AUniqueIdentifier.getDeviceId(getActivity())+"&level=one"+"&start="+start+"&length="+"10"
                +"&userName="+tmall_teamone_editText_userName.getText().toString()+"&userPhone="+tmall_teamone_editText_userPhone.getText().toString()
                +"&startTime="+tmall_teamone_editText_startTime.getText().toString()+"&endTime="+tmall_teamone_editText_endTime.getText().toString());
        Map<String, String> teamMap2=new HashMap<>();
        teamMap2.put("token",SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        teamMap2.put("appId",AUniqueIdentifier.getDeviceId(getActivity()));
        teamMap2.put("level","one");
        teamMap2.put("start",start+"");
        teamMap2.put("length","10");
        teamMap2.put("userName",tmall_teamone_editText_userName.getText().toString());
        teamMap2.put("userPhone",tmall_teamone_editText_userPhone.getText().toString());
        teamMap2.put("startTime",tmall_teamone_editText_startTime.getText().toString());
        teamMap2.put("endTime",tmall_teamone_editText_endTime.getText().toString());
        OkHttpUtils.post().params(teamMap2)
        .url(TEAMURL)
        .build()
        .execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                teamBeanlist.clear();
                Gson gson=new Gson();
                TeamBean teaminquireBean = gson.fromJson(response, TeamBean.class);
                teamBeanlist.addAll(teaminquireBean.getData().getList());
            }
        });
   }



    private void initTeamHttp() {
        Log.e("我的团队-一级好友",TEAMURL+"?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)
                +"&appId="+AUniqueIdentifier.getDeviceId(getActivity())+"&level=one"+"&start="+start+"&length="+"10");
        teamMap = new HashMap<>();
        teamMap.put("token",SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        teamMap.put("appId",AUniqueIdentifier.getDeviceId(getActivity()));
        teamMap.put("level","one");
        teamMap.put("start",start+"");
        teamMap.put("length","10");

        OkHttpUtils.post().params(teamMap).url(TEAMURL).build().execute(new StringCallback(){
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                tmall_teamoneRecyclerView.refreshComplete();
                initData(response);

            }
        });
    }


    private void initData(String response) {
        Gson gson = new Gson();
        TeamBean TeamBean = gson.fromJson(response, TeamBean.class);
        if (TeamBean.getCode().equals("10000")) {
            if (TeamBean != null && TeamBean.getData() != null && TeamBean.getData().getList() != null) {
                if (start == 0) {
                    teamBeanlist.clear();
                }
                teamBeanlist.addAll(TeamBean.getData().getList());
                tmall_teamoneFragmentAdapter.notifyDataSetChanged();
            }
        }
        dialog.dismiss();
    }


    private void initView() {
        teamBeanlist = new ArrayList<TeamBean.DataBean.ListBean>();
        tmall_teamoneRecyclerView = (XRecyclerView) inflate.findViewById(R.id.tmall_teamoneRecyclerView);
        tmall_teamoneFragmentAdapter = new tmall_teamoneFragmentAdapter(teamBeanlist, getActivity());
        mLayoutManager = new LinearLayoutManager(getActivity());
        tmall_teamoneRecyclerView.setLayoutManager(mLayoutManager);
        tmall_teamoneRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                start = 0;
                initTeamHttp();
            }

            @Override
            public void onLoadMore() {
                start++;
                initTeamHttp();
            }
        });
        initTeamHttp();
        tmall_teamoneRecyclerView.setAdapter(tmall_teamoneFragmentAdapter);
        tmall_teamone_editText_userName = (EditText) inflate.findViewById(R.id.tmall_teamone_EditText_userName);
        tmall_teamone_editText_userPhone = (EditText) inflate.findViewById(R.id.tmall_teamone_EditText_userPhone);
        tmall_teamone_editText_startTime = (EditText) inflate.findViewById(R.id.tmall_teamone_EditText_startTime);
        tmall_teamone_editText_endTime = (EditText) inflate.findViewById(R.id.tmall_teamone_EditText_endTime);
        tmall_teamone_editText_inquire = (Button) inflate.findViewById(R.id.tmall_teamone_EditText_inquire);

        tmall_teamone_editText_inquire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initTeamInquireHttp();
                tmall_teamoneFragmentAdapter.notifyDataSetChanged();
            }
        });

    }
}
